package com.app.utils;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.BitmapManager;
import com.eyzhs.app.R;
import com.lepu.app.fun.chat.business.AppCommonService;
import com.lepu.app.fun.market.IntegralAnimaActivity;
import com.lepu.app.usercenter.LoginActivity;
import com.lepu.app.usercenter.UserInfoActivity;
import com.lepu.app.usercenter.UserOtherInfoActivity;
import com.lepu.app.usercenter.bean.LoginInfo;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;

import java.io.File;

/*
 * 登录检查封装
 */
public class LoginUtil {

    public static boolean jumpLogin(BaseFragmentActivity baseFragmentActivity) {
        boolean flag = false;
        if (!checkIsLogin()) {
            Intent intent = new Intent(baseFragmentActivity,
                    LoginActivity.class);
            baseFragmentActivity.startActivityAnim(intent,
                    R.anim.lib_push_down_up_in, R.anim.lib_push_down_up_out);
            flag = true;
        }
        return flag;
    }

    public static boolean checkIsLogin() {
        boolean flag = false;

        LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
        if (loginInfo != null && !TextUtils.isEmpty(loginInfo.LoginToken)) {
            flag = true;
        }

        return flag;
    }

    public static boolean checkIsBond() {
        boolean flag = false;

        LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
        if (loginInfo != null && !TextUtils.isEmpty(loginInfo.MobilePhone)) {
            flag = true;
        }

        return flag;
    }

    public static String getUserId() {
        String userId = "-1";

        if (checkIsLogin()) {
            userId = MyApplication.getInstance().getLoginInfo().UserID;
        }
        return userId;
    }

    public static String getUserAvare() {
        String avatar = "";

        if (checkIsLogin()) {
            avatar = MyApplication.getInstance().getLoginInfo().Avatar;
        }
        return avatar;
    }

    public static String getLoginToken() {
        String token = "";

        if (checkIsLogin()) {
            token = MyApplication.getInstance().getLoginInfo().LoginToken;
        }
        return token;
    }

    public static boolean isOldUser(String nickName) {
        boolean flag = false;

        if (checkIsLogin()) {
            String phone = MyApplication.getInstance().getLoginInfo().MobilePhone;
            if (!TextUtils.isEmpty(phone)
                    && !TextUtils.isEmpty(nickName)
                    && nickName.equals(phone)) {
                flag = true;
            }
        }

        return flag;
    }

    public static void loginOut() {
        MyApplication.getInstance().setNormalLoginout(true);
        UserConfig.setConfig(MyApplication.getInstance(),
                Const.CONFIG_APP_LOGIN_INFO, "");
        MyApplication.getInstance().setLoginInfo(null);
        //set chatsession_message
        AppCommonService.getInstance().logoutXGXMPP();

    }
    
    public static void jumpUserInfo(BaseFragmentActivity baseFragmentActivity, String userId) {
        if (!checkIsLogin()) {
            Intent intent = new Intent(baseFragmentActivity,LoginActivity.class);
            baseFragmentActivity.startActivityAnim(intent,R.anim.lib_push_down_up_in, R.anim.lib_push_down_up_out);
        }else{
        	if(userId.equals(getUserId())){
        		Intent intent = new Intent(baseFragmentActivity,UserInfoActivity.class);
                baseFragmentActivity.startActivity(intent,true);
        	}else{
        		Intent intent = new Intent(baseFragmentActivity,UserOtherInfoActivity.class);
				intent.putExtra("targetId", userId);
				baseFragmentActivity.startActivity(intent,true);
        	}
        }
    }
    
    public static void jumpIntegral(BaseFragmentActivity baseFragmentActivity, int integral) {
    	if(integral == 0){
    		return;
    	}
    	Intent intentAnima = new Intent(baseFragmentActivity,IntegralAnimaActivity.class);
		intentAnima.putExtra("integral", String.valueOf(integral));
		baseFragmentActivity.startActivity(intentAnima);
    }

    public static void clearUserIcon(Context context) {
        String filePath = SdLocal.getUserPath(context, LoginUtil.getUserId());
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        BitmapManager.clearBitmapMemoryCache();
    }

    /**
     * 加载用户头像
     * @param context
     * @param iconImageView
     * @param url
     */
    public static void loadSelfUserImage(Context context,
                                         ImageView iconImageView,String url) {

        String imageUrl = Setting.getImageUrl(url);
        HeadIconLoadUtil.displayImage(R.drawable.app_ic_default_avatar, imageUrl, iconImageView);
    }

    public static void loadSelfUserImage(Context context,
                                         ImageView iconImageView,String url,int resDefaultId) {

        String imageUrl = Setting.getImageUrl(url);
        HeadIconLoadUtil.displayImage(resDefaultId, imageUrl, iconImageView);
    }
}
